﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;
using BIS.Utility.UrlRewrite;


namespace BIS.Portal.Modules.CMS
{
    public partial class CmsArticleByHotNew : ControlBase
    {

        #region Lấy dữ liệu settings
        private string NoiDungHienThi
        {
            get { return GetControlSetting<string>(ModuleSettingNames.CmsArticleByHotNew.DDLNoiDungHienthi, PortalContext.LanguageAlias); }
        }
        private bool LocQueryString
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLLocTheoQuery, PortalContext.LanguageAlias); }
        }
        private bool HienThiTinNoiBatB
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLTinNoiBatHot, PortalContext.LanguageAlias); }
        }
        private bool HienThiAnhMinhHoaTinNoiBat
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLAnhMinhHoaHot, PortalContext.LanguageAlias); }
        }
        private bool HienThiMotaTinNoiBat
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLMieuTaHot, PortalContext.LanguageAlias); }
        }
        private bool HienThiLinkCtTinNoiBat
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLHienThiLinkHot, PortalContext.LanguageAlias); }
        }
        private bool HienThiTinMoiB
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLTinMoi, PortalContext.LanguageAlias); }
        }
        private bool HienThiAnhMinhHoaTinMoiB
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLAnhMinhHoaTinMoi, PortalContext.LanguageAlias); }
        }
        private bool HienThiMotaTinMoiB
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLHienThiMotaTinMoi, PortalContext.LanguageAlias); }
        }
        private bool HienThiLinkCtTinMoiB
        {
            get { return GetControlSetting<bool>(ModuleSettingNames.CmsArticleByHotNew.DDLHienThiLinkTinMoi, PortalContext.LanguageAlias); }
        }
        private int SoLuongHienThiTinNoiBat
        {
            get { return GetControlSetting<int>(ModuleSettingNames.CmsArticleByHotNew.DDLSoLuongTinHot, PortalContext.LanguageAlias); }
        }
        private int SoLuongHienThiTinMoi
        {
            get { return GetControlSetting<int>(ModuleSettingNames.CmsArticleByHotNew.DDLSoluongTinMoi, PortalContext.LanguageAlias); }
        }
        #endregion

        
        private string GetCateName(int iCateId)
        {
            return ClientCmsArticleCategoryAndCategoryBO.GetCategoryNameById(iCateId).Category_name;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            if (NoiDungHienThi!="")
            {
            lnkTitle.Text = GetCateName(Functions.GetInt(NoiDungHienThi));
            }
            if (HienThiTinNoiBatB)
            {
                IList<ClientCmsArticleAndCategoryEntity> list = null;
                #region Lấy categoryId theo QueryString
                if (LocQueryString)
                {

                    if (Request[ModuleQueryStringKey.CategoryId] == null || Request[ModuleQueryStringKey.CategoryId] == "0")
                    {
                        list = ClientCmsArticleBO.GetArticleByHotArticle(1, PortalContext.LanguageInUseId, 0, SoLuongHienThiTinNoiBat);
                    }
                    else
                    {
                        list = ClientCmsArticleBO.GetArticleByHotArticle(1, PortalContext.LanguageInUseId, Convert.ToInt32(Request[ModuleQueryStringKey.CategoryId]), SoLuongHienThiTinNoiBat);
                    }
                }
                else
                {
                    list = ClientCmsArticleBO.GetArticleByHotArticle(1, PortalContext.LanguageInUseId, Convert.ToInt32(NoiDungHienThi), SoLuongHienThiTinNoiBat);
                }
                #endregion
                Tin_Noi_Bat.DataSource = list;
                Tin_Noi_Bat.DataBind();
            }
            if (HienThiTinMoiB)
            {
                IList<ClientCmsArticleAndCategoryEntity> list = null;
                #region Lấy categoryId theo QueryString
                if (LocQueryString)
                {
                    if (Request[ModuleQueryStringKey.CategoryId] != null || Request[ModuleQueryStringKey.CategoryId] == "0")
                    {
                        list = ClientCmsArticleBO.GetArticleNewByDate(0, PortalContext.LanguageInUseId, SoLuongHienThiTinMoi);
                    }
                    else
                    {
                        list = ClientCmsArticleBO.GetArticleNewByDate(Convert.ToInt32(Request[ModuleQueryStringKey.CategoryId]), PortalContext.LanguageInUseId, SoLuongHienThiTinMoi);
                    }
                }
                else
                {
                    list = ClientCmsArticleBO.GetArticleNewByDate(Convert.ToInt32(NoiDungHienThi), PortalContext.LanguageInUseId, SoLuongHienThiTinMoi);
                }
                #endregion
                Tin_Moi.DataSource = list;
                Tin_Moi.DataBind();
            }

        }


        protected void Tin_Noi_Bat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                if (null != objArticle)
                {
                    #region Url Chi Tiết
                    string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.Page_alias,
                                        new string[]
                                            {
                                           ModuleQueryStringKey.CategoryId,
                                           ModuleQueryStringKey.ArticleId,
                                           ModuleQueryStringKey.CategoryName,
                                           ModuleQueryStringKey.ArticleTile
                                           },
                                        new string[]
                                            {
                                           Convert.ToString(objArticle.Category_id),
                                           Convert.ToString(objArticle.Article_id),
                                           Functions.UnicodeToKoDauAndGach(GetCateName(objArticle.Category_id)),
                                           Functions.UnicodeToKoDauAndGach(objArticle.Article_title)
                                           }
                    );

                    #endregion
                   
                    #region Bind data Image
                    if (HienThiAnhMinhHoaTinNoiBat)
                    {
                        Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa") as Image;
                        if (imgAnh != null)
                        {
                            imgAnh.ImageUrl = objArticle.Thumb_image;
                            imgAnh.AlternateText = objArticle.Article_title;
                            imgAnh.Attributes.Add("onerror", "ImageNotFound(this);");
                        }
                    }
                    #endregion
                    #region Bind data Link Tiêu đề
                    HyperLink lnkTieuDe = e.Item.FindControl("Tieu_De") as HyperLink;
                    if (lnkTieuDe != null)
                    {
                        lnkTieuDe.ToolTip = objArticle.Article_title;
                        lnkTieuDe.NavigateUrl = urlChiTiet;
                        lnkTieuDe.Text = objArticle.Article_title;
                    }
                    #endregion
                    #region Bind Nội dung trong Literal
                    if (HienThiMotaTinNoiBat)
                    {
                        Literal ltrNoiDung = e.Item.FindControl("Noi_Dung") as Literal;
                        if (ltrNoiDung != null) ltrNoiDung.Text = objArticle.Article_description;
                    }
                    #endregion
                    #region Bind data Link Xem tiếp

                    if (HienThiLinkCtTinNoiBat)
                    {
                        HyperLink lnkXemTiep = e.Item.FindControl("Link_Xem_Tiep") as HyperLink;
                        if (lnkXemTiep != null)
                        {
                            lnkXemTiep.Text = GetLanguageValue("ReadMoreButton");
                            lnkXemTiep.NavigateUrl = urlChiTiet;
                        }
                    }

                    #endregion

                }
            }
        }

        protected void Tin_Moi_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                if (null != objArticle)
                {
                    #region Url Chi Tiết
                    string urlChiTiet = WebHelpers.GetLink(ModuleQueryStringKey.UrlLinkChiTiet, objArticle.Page_alias,
                                        new string[]
                                            {
                                           ModuleQueryStringKey.CategoryId,
                                           ModuleQueryStringKey.ArticleId,
                                           ModuleQueryStringKey.CategoryName,
                                           ModuleQueryStringKey.ArticleTile
                                           },
                                        new string[]
                                            {
                                           Convert.ToString(objArticle.Category_id),
                                           Convert.ToString(objArticle.Article_id),
                                           Functions.UnicodeToKoDauAndGach(GetCateName(objArticle.Category_id)),
                                           Functions.UnicodeToKoDauAndGach(objArticle.Article_title)
                                           }
                    );

                    #endregion
                 
                    #region Bind data Image
                    if (HienThiAnhMinhHoaTinMoiB)
                    {
                        Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa_TMoi") as Image;
                        if (imgAnh != null)
                        {
                            imgAnh.ImageUrl = objArticle.Thumb_image;
                            imgAnh.AlternateText = objArticle.Article_title;
                            imgAnh.Attributes.Add("onerror", "ImageNotFound(this);");
                        }
                    }
                    #endregion
                    #region Bind data Link Tiêu đề
                    HyperLink lnkTieuDe = e.Item.FindControl("Tieu_De_TMoi") as HyperLink;
                    if (lnkTieuDe != null)
                    {
                        lnkTieuDe.ToolTip = objArticle.Article_title;
                        lnkTieuDe.NavigateUrl = urlChiTiet;
                        lnkTieuDe.Text = objArticle.Article_title;
                    }
                    #endregion
                    #region Bind Nội dung trong Literal
                    if (HienThiMotaTinMoiB)
                    {
                        Literal ltrNoiDung = e.Item.FindControl("Noi_Dung_TMoi") as Literal;
                        if (ltrNoiDung != null) ltrNoiDung.Text = objArticle.Article_description;
                    }
                    #endregion
                    #region Bind data Link Xem tiếp

                    if (HienThiLinkCtTinMoiB)
                    {
                        HyperLink lnkXemTiep = e.Item.FindControl("Link_Xem_Tiep_TMoi") as HyperLink;
                        if (lnkXemTiep != null)
                        {
                            lnkXemTiep.Text = GetLanguageValue("ReadMoreButton");
                            lnkXemTiep.NavigateUrl = urlChiTiet;
                        }
                    }

                    #endregion

                }
            }
        }
    }
}